package com.woniuxy.orderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.common.PageResult;
import com.woniuxy.common.Result;
import com.woniuxy.form.OrderForm;
import com.woniuxy.form.OrderTransferForm;
import com.woniuxy.orderassembly.model.Order;
import com.woniuxy.orderassembly.dao.OrderMapper;
import com.woniuxy.orderassembly.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chestnut
 * @since 2021-05-24
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Resource
    private OrderMapper orderMapper;

    /**
     * 用户下单后调用平台生成订单
     * 生成订单后 发送信息至酒店
     * @param orderForm
     * @return
     */
    @Override
    public Result createOrder(OrderForm orderForm) {
        Order order = new Order();

        log.info("生成订单信息,{}",orderForm);
        /**
         * 默认设置orderException 为0:无状态
         * 默认设置status 为1:待支付
         * orderTime 获取当前时间戳
         */
        BeanUtils.copyProperties(orderForm,order);
        order.setOrderException(0);
        order.setStatus(1);
        LocalDateTime localDateTime = new Date()
                .toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        order.setOrderTime(localDateTime);
        int i = orderMapper.insert(order);
        return i>0?Result.success():Result.fail("下单失败");
    }

    /**
     *酒店条件查询订单
     *
     * @param hotelId
     * @param status
     * @param pageIdx
     * @return
     */
    @Override
    public Result hotelQueryOrderPage(Integer hotelId, Integer status, Integer pageIdx) {
        //查询条件
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        //根据用户id查
        wrapper.eq("hotel_id",hotelId).eq("status",status);

        //当前页码，每页条数
        Page<Order> pageParam = new Page(pageIdx,pageIdx);
        IPage<Order> pageResult = orderMapper.selectPage(pageParam,wrapper);

        //将mybatis-plus的结果转为我自定义的PageResult
        PageResult<Order> myPageResult =new PageResult<Order>((int)pageResult.getTotal(),(int)pageResult.getCurrent(),(int)pageResult.getSize(),pageResult.getRecords());
        myPageResult.setSuccess(true);
        return myPageResult;
    }

    /**
     * 用户条件查询订单
     * @param userId
     * @param status
     * @param pageIdx
     * @return
     */
    @Override
    public Result userQueryOrderPage(Integer userId, Integer status, Integer pageIdx) {
        //查询条件
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        //根据用户id查
        wrapper.eq("user_id",userId).eq("status",status);
        //当前页码，每页条数
        Page<Order> pageParam = new Page(pageIdx,pageIdx);
        IPage<Order> pageResult = orderMapper.selectPage(pageParam,wrapper);

        //将mybatis-plus的结果转为我自定义的PageResult
        PageResult<Order> myPageResult =new PageResult<Order>((int)pageResult.getTotal(),(int)pageResult.getCurrent(),(int)pageResult.getSize(),pageResult.getRecords());
        myPageResult.setSuccess(true);
        return myPageResult;
    }

    /**
     * 酒店表示给出处理信息（true/false）
     * 调用该接口
     * @param orderId
     * @return
     */
    @Override
    public Result hotelManageOrder(Integer orderId,Boolean bool) {
        QueryWrapper<Object> objectQueryWrapper = new QueryWrapper<>();
        Order order = orderMapper.selectById(orderId);
        //根据传进来的bool 修改订单状态表示 拒绝订单11 接受订单00
        if(bool){
            order.setStatus(00);
        }
        order.setStatus(11);
        return null;
    }

    @Override
    public List<OrderTransferForm> findOrderByStatus() {
        //状态为4的时候为已完成订单
        List<OrderTransferForm> orders = orderMapper.findOrderByStatus(4);
        return orders;
    }

    @Override
    public void modifyOrderStatus(Integer status ,Integer orderId) {
        orderMapper.modifyOrderStatus(status,orderId);
    }


}
